<?php

namespace App\Services;
use Illuminate\Support\Facades\Validator;

class ValidatorService {
    public function checkValidator($rule, $data, $names = [], $messages = [])
    {
        $default_messages = [
            'required'=> ':attribute为必填项',
            'min' => ':attribute为常规的',
            'email' => ':attribute格式错误',
            'string' => ':attribute为字符串',
            'numeric' => ":attribute为数字",
            'array' => ":attribute为数组"
        ];
        $rule_messages = array_merge($default_messages, $messages);

        $default_names = [
            'wd_uid' => '项目UID',
            'test_report_uid' => '测试单唯一标识UUID',
            'pwd' => '密码',
            'idno' => '身份证号',
            'name' => '名称',
            'phone' => '手机号',
            'member_id' => '用户ID',
            'province_id' => '省份ID',
            'type' => '类型',
            'remark' => '备注',
            'status' => '状态'
        ];

        $rule_names = array_merge($default_names, $names);
        $validator = Validator::make($data, $rule, $rule_messages, $rule_names);
        $msg = '';
        if ($validator->fails()) {
            $message = $validator->errors();
            foreach ($message->all() as $message) {
                $msg .= $message.",";
            }
        }
        if($msg) {
            return ['code' => 201, 'data' => rtrim($msg, ",")];
        }
        return ['code' => 200, 'data' => 'ok'];
    }

}
